package com.sanery.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sanery.dao.UserDaoImpl;
import com.sanery.entity.User;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/pages/login.html").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserDaoImpl dao = new UserDaoImpl();
		User user = dao.login(username, password);
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("cur_user", user);
			response.sendRedirect("list");
		} else {
			doGet(request, response);
		}
	}

}
